﻿using System;
using Microsoft.SharePoint;

namespace SPRepository
{
    public class DocumentContentType : ItemContentType, IDocumentContentType<DocumentContentTypeDto>
    {
        #region FieldTypes

        public SimpleFieldType<string> DocumentCreatedBy
        {
            get { return new SimpleFieldType<string>(SPBuiltInFieldId.Created_x0020_By); }
        }

        public SimpleFieldType<string> DocumentModifiedBy
        {
            get { return new SimpleFieldType<string>(SPBuiltInFieldId.Modified_x0020_By); }
        }

        public SimpleFieldType<string> FileName
        {
            get { return new SimpleFieldType<string>(SPBuiltInFieldId.FileLeafRef); }
        }

        #endregion

        public new DocumentContentTypeDto Get(object item)
        {
            var spListItem = item as SPListItem;
            ItemContentTypeDto baseDto = base.Get(spListItem);

            var docCreatedBy = DocumentCreatedBy.GetValue(spListItem);
            var docModifiedBy = DocumentModifiedBy.GetValue(spListItem);
            var fileName = FileName.GetValue(spListItem);

            return new DocumentContentTypeDto(docCreatedBy, docModifiedBy, fileName, baseDto);
        }

        public void Update(object item, DocumentContentTypeDto dto)
        {
            base.Update(item, dto);

            var spListItem = item as SPListItem;
            DocumentCreatedBy.SetValue(spListItem, dto.DocumentCreatedBy);
            DocumentModifiedBy.SetValue(spListItem, dto.DocumentModifiedBy);
            FileName.SetValue(spListItem, dto.FileName);
        }

        public override string GetContentTypeIdDefinition()
        {
            return SPBuiltInContentTypeId.Document.ToString();
        }
    }
}